/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package error;

import controller.cCheckerboard;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import view.iDraughts;

/**
 *
 * @author Robert
 */
public class ConfirmationDialog {

    /**
     * @param title : Title of the JDialog
     * @param msg   : Message to display
     * @param frame : Current frame containing JDialog
     * @param action: Surrender, new game...
     */
    public ConfirmationDialog(String title, String msg, iDraughts frame, cCheckerboard cb, int action){
        int result = JOptionPane.showConfirmDialog(frame, msg, title,JOptionPane.YES_NO_OPTION);
        switch(action)
        {
            case 0 : // New game
                newGame(result, cb, frame);
            break;
            case 1 : // Surrender
                surrender(result, cb, frame);
            break;
            default: break;
        }
    }

    private void newGame(int result, cCheckerboard cb, JFrame frame)
    {
       // if Click on yes, result = 0
       // if Click on no, result = 1
        if(result == 0)
        {
            frame.dispose();
            frame = new iDraughts();
        }
    }

    private void surrender(int result, cCheckerboard cb, JFrame frame)
    {
       // if Click on yes, result = 0
       // if Click on no, result = 1
        if(result == 0)
        {
            // The current player surrender so winner is the other one
            String joueur = Integer.toString(cb.getCheckerboard().getPlayer());
            JOptionPane.showMessageDialog(frame, "Le Joueur " + joueur + " a perdu", "Abandon", JOptionPane.INFORMATION_MESSAGE);
            newGame(result, cb, frame);
        }
    }
}
